package com.yzh.luckydraw.interfaces.facade;

import com.alibaba.fastjson.JSON;
import com.yzh.luckydraw.application.process.draw.IActivityProcess;
import com.yzh.luckydraw.application.process.draw.req.DrawProcessReq;
import com.yzh.luckydraw.application.process.draw.res.DrawProcessResult;
import com.yzh.luckydraw.application.process.draw.res.RuleQuantificationCrowdResult;
import com.yzh.luckydraw.common.Constants;
import com.yzh.luckydraw.domain.rule.model.req.DecisionMatterReq;
import com.yzh.luckydraw.domain.strategy.model.vo.DrawAwardVO;
import com.yzh.luckydraw.interfaces.assembler.AwardMapping;
import com.yzh.luckydraw.rpc.activity.booth.ILuckyDrawActivityBooth;
import com.yzh.luckydraw.rpc.activity.booth.dto.AwardDTO;
import com.yzh.luckydraw.rpc.activity.booth.req.DrawReq;
import com.yzh.luckydraw.rpc.activity.booth.req.QuantificationDrawReq;
import com.yzh.luckydraw.rpc.activity.booth.res.DrawRes;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;

@DubboService
public class LuckyDrawActivityBooth implements ILuckyDrawActivityBooth {
    private Logger logger = LoggerFactory.getLogger(LuckyDrawActivityBooth.class);
    @Resource
    private IActivityProcess activityProcess;
    @Resource
    private AwardMapping awardMapping;

    @Override
    public DrawRes doDraw(DrawReq req) {
        try {
            logger.info("抽奖开始 uId:{} activityId:{}", req.getuId(), req.getActivityId());
            //1.执行抽奖
            DrawProcessResult drawProcessResult = activityProcess.doDrawProcess(new DrawProcessReq(req.getuId(), req.getActivityId()));
            if (!drawProcessResult.getCode().equals(Constants.ResponseCode.SUCCESS.getCode())) {
                logger.error("抽奖失败(过程异常) uId:{}, activityId:{}", req.getuId(), req.getActivityId());
                return new DrawRes(drawProcessResult.getCode(), drawProcessResult.getInfo());
            }

            //2.数据转换
            DrawAwardVO drawAwardVO = drawProcessResult.getDrawAwardInfo();
            AwardDTO awardDTO = awardMapping.toDTO(drawAwardVO);
            awardDTO.setActivityId(req.getActivityId());

            //3.封装数据
            DrawRes drawRes = new DrawRes(Constants.ResponseCode.SUCCESS.getCode(), Constants.ResponseCode.SUCCESS.getInfo());
            drawRes.setAwardDTO(awardDTO);
            logger.info("抽奖完成 uId:{} activityId:{} DrawRes:{}", req.getuId(), req.getActivityId(), JSON.toJSONString(drawRes));
            return drawRes;
        } catch (Exception e) {
            logger.error("抽奖失败 uId:{} activityId:{} DrawReq:{}", req.getuId(), req.getActivityId(), JSON.toJSONString(req), e);
            return new DrawRes(Constants.ResponseCode.UN_ERROR.getCode(), Constants.ResponseCode.UN_ERROR.getInfo());
        }

    }

    @Override
    public DrawRes doQuantificationDraw(QuantificationDrawReq req) {
        try {
            logger.info("量化人群抽奖开始 uId:{} treeId:{}", req.getuId(), req.getTreeId());
            //1.执行规则引擎,获取用户可以参与的活动号
            RuleQuantificationCrowdResult ruleQuantificationCrowdResult = activityProcess.doRuleQuantificationCrowd(new DecisionMatterReq(req.getuId(), req.getTreeId(), req.getValMap()));
            if (!Constants.ResponseCode.SUCCESS.getCode().equals(ruleQuantificationCrowdResult.getCode())) {
                logger.error("量化人群抽奖失败(规则引擎异常) uId:{}, treeId:{}", req.getuId(), req.getTreeId());
                return new DrawRes(ruleQuantificationCrowdResult.getCode(), ruleQuantificationCrowdResult.getInfo());
            }

            //2.执行抽奖
            DrawProcessResult drawProcessResult = activityProcess.doDrawProcess(new DrawProcessReq(req.getuId(), ruleQuantificationCrowdResult.getActivityId()));
            if (!Constants.ResponseCode.SUCCESS.getCode().equals(drawProcessResult.getCode())) {
                logger.error("抽奖失败(过程异常) uId:{}, activityId:{}", req.getuId(), ruleQuantificationCrowdResult.getActivityId());
                return new DrawRes(drawProcessResult.getCode(), drawProcessResult.getInfo());
            }

            //3.数据转换
            AwardDTO awardDTO = awardMapping.toDTO(drawProcessResult.getDrawAwardInfo());
            awardDTO.setActivityId(ruleQuantificationCrowdResult.getActivityId());

            //4.封装数据
            DrawRes drawRes = new DrawRes(Constants.ResponseCode.SUCCESS.getCode(), Constants.ResponseCode.SUCCESS.getInfo());
            drawRes.setAwardDTO(awardDTO);

            logger.info("量化人群抽奖完成 uId:{} treeId:{} drawRes:{}", req.getuId(), req.getTreeId(), JSON.toJSONString(drawRes));
            return drawRes;
        } catch (Exception e) {
            logger.error("量化人群抽奖失败 uid:{} treeId:{} req:{}", req.getuId(), req.getTreeId(), JSON.toJSONString(req));
            return new DrawRes(Constants.ResponseCode.UN_ERROR.getCode(), Constants.ResponseCode.UN_ERROR.getInfo());
        }
    }
}
